package com.imooc.service.impl;

import com.imooc.enums.Cats;
import com.imooc.mapper.CategoryMapper;
import com.imooc.mapper.CategoryMapperCustom;
import com.imooc.pojo.Category;
import com.imooc.pojo.vo.CategoryVO;
import com.imooc.pojo.vo.NewItemsVO;
import com.imooc.service.CategoryService;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;

/**
 * @ClassName: CategoryServiceImpl
 * @Package: com.imooc.service.impl
 * @Description:
 * @Date: 2021/7/7 15:22
 * @Author: sunzheng@beiming.com
 */
@Service
public class CategoryServiceImpl implements CategoryService {

  @Autowired
  private CategoryMapper categoryMapper;

  @Autowired
  private CategoryMapperCustom categoryMapperCustom;

  @Transactional(propagation = Propagation.SUPPORTS)
  @Override
  public List<NewItemsVO> getSixNewItemsLazy(Integer rootCatId) {
    Map<String, Object> map = new HashMap<>();
    map.put("rootCatId", rootCatId);
    return categoryMapperCustom.getSixNewItemsLazy(map);
  }

  @Transactional(propagation = Propagation.SUPPORTS)
  @Override
  public List<CategoryVO> getSubCatList(Integer rootCatId) {
    return categoryMapperCustom.getSubCatList(rootCatId);
  }

  @Transactional(propagation = Propagation.SUPPORTS)
  @Override
  public List<Category> queryAllRootLevelCat() {
    Example example = new Example(Category.class);
    Criteria criteria = example.createCriteria();
    criteria.andEqualTo("type", Cats.level1.type);
    List<Category> result = categoryMapper.selectByExample(example);
    return result;
  }
}
